package cn.zhqr.lib.utils;

import cn.zhqr.common.util.str.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class StringUtil {
    private static final String default_separator_char = ",";

    public static boolean isBlank(String str) {
        return str == null || str.trim().length() == 0;
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static List<String> split(String str, String separator_char) {
        return split(str, separator_char, false);
    }

    public static List<String> split(String str, String separator_char, boolean distinct) {
        if (isBlank(str)) {
            return new ArrayList();
        } else {
            List<String> stringList = new ArrayList();
            List<String> var4 = StringUtils.split(str, separator_char);
            int var5 = var4.size();

            for(int var6 = 0; var6 < var5; ++var6) {
                String s = var4.get(var6);
                if (!distinct || !stringList.contains(s)) {
                    stringList.add(s);
                }
            }

            return stringList;
        }
    }

    public static List<String> split(String str) {
        return split(str, ",");
    }

    public static List<Integer> split2Int(String str, String separator_char, boolean distinct) {
        if (StringUtils.isBlank(str)) {
            return new ArrayList();
        } else {
            List<Integer> intList = new ArrayList();
            List<String> var4 = StringUtils.split(str, separator_char);
            int var5 = var4.size();

            for(int var6 = 0; var6 < var5; ++var6) {
                String s = var4.get(var6);
                if (!StringUtils.isBlank(s)) {
                    try {
                        Integer i = Integer.parseInt(s.trim());
                        if (!distinct || !intList.contains(i)) {
                            intList.add(i);
                        }
                    } catch (Exception var9) {
                        System.out.println("Failed to parse string '" + s + "' to int!");
                    }
                }
            }

            return intList;
        }
    }

    public static List<Integer> split2Int(String str, String separator_char) {
        return split2Int(str, separator_char, true);
    }

    public static List<Integer> split2Int(String str) {
        return split2Int(str, ",");
    }

    public static List<Long> split2Long(String str, String separator_char, boolean distinct) {
        if (StringUtils.isBlank(str)) {
            return new ArrayList();
        } else {
            List<Long> intList = new ArrayList();
            List<String> var4 = StringUtils.split(str, separator_char);
            int var5 = var4.size();

            for(int var6 = 0; var6 < var5; ++var6) {
                String s = var4.get(var6);
                if (!StringUtils.isBlank(s)) {
                    try {
                        Long i = Long.parseLong(s.trim());
                        if (!distinct || !intList.contains(i)) {
                            intList.add(i);
                        }
                    } catch (Exception var9) {
                        System.out.println("Failed to parse string '" + s + "' to Long!");
                    }
                }
            }

            return intList;
        }
    }

    public static List<Long> split2Long(String str, String separator_char) {
        return split2Long(str, separator_char, true);
    }

    public static List<Long> split2Long(String str) {
        return split2Long(str, ",");
    }

    public static String repeatStr(String str, int n) {
        String result = ""; // 创建一个空的字符串变量
        for (int i = 0; i < n; i++) { // 使用循环重复 n 次
            result += str; // 将原始字符串拼接到 result 变量上
        }
        return result; // 返回 result 变量作为结果
    }

    /**
     * 敏感字符脱敏
     * @param srcStr
     * @param startIndex
     * @param endIndex
     * @return
     */
    public static String desensitize(String srcStr,int startIndex,int endIndex){
        if(isBlank(srcStr)){
            return "";
        }
        if(endIndex < startIndex){
            return "";
        }
        if(endIndex <= 0) return srcStr;

        if(startIndex <= 0) startIndex = 0;
        if(startIndex > srcStr.length()-1){
            return "";
        }

        StringBuilder sb = new StringBuilder();
        sb.append(srcStr, 0, startIndex);
        sb.append(repeatStr("*",endIndex-startIndex));
        if(endIndex <= srcStr.length()-1){
            sb.append(srcStr.substring(endIndex));
        }
        return sb.toString();
    }

    public static String subStr(String src, int maxlength){
        if(StringUtils.isBlank(src) || src.length() <= maxlength){
            return src;
        }
        return src.substring(0, maxlength);
    }
}
